package com.li;

import java.util.*;

/**
 * @author lirenchun
 */
public class ContinuousEveryCharNum {
    /**
     * 获取字符串中*各个字符连续及其连续出现次数*Map
     *
     * @param str
     * @return
     */
    public static HashMap<Character, Integer> getCharContinuousNumByMap(String str) {
        HashMap<Character, Integer> map = new HashMap<>();
        if (str.length() == 1) {
            map.put(str.charAt(0), 1);
            return map;
        } else {
            int c = 1;
            for (int i = 0; i < str.length() - 1; i++) {
                if (str.charAt(i) == str.charAt(i + 1)) {
                    c++;
                } else {
                    if (map.get(str.charAt(i)) != null) {
                        if (c > map.get(str.charAt(i))) {
                            map.put(str.charAt(i), c);
                            c = 1;
                        }
                    } else {
                        map.put(str.charAt(i), c);
                        c = 1;
                    }
                }
            }
            if (map.get(str.charAt(str.length() - 1)) != null) {
                if (c > map.get(str.charAt(str.length() - 1))) {
                    map.put(str.charAt(str.length() - 1), c);
                }
            } else {
                map.put(str.charAt(str.length() - 1), c);
            }
            return map;
        }
    }

    /**
     * 获取字符串中最大连续出现字符的次数*Integer
     *
     * @param str
     * @return
     */
    public static Integer getCharContinuousMaxNum(String str) {
        if (str.length() == 0) {
            return 0;
        }
        int maxNum = 0;
        if (str.length() == 1) {
            maxNum = 1;
            return maxNum;
        } else {
            int c = 1;
            for (int i = 0; i < str.length() - 1; i++) {
                char s = str.charAt(i);
                if (str.charAt(i) == str.charAt(i + 1)) {
                    c++;
                } else {
                    if (c > maxNum) {
                        maxNum = c;
                        c = 1;
                    }
                }
            }
            if (c > maxNum) {
                maxNum = c;
            }
            return maxNum;
        }
    }

    public static void main(String[] args) {
        String str = "车12";
        System.out.println(getCharContinuousMaxNum(str));
    }
}
